package com.zsck.pos.common;

import android.view.View;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ClickListenerProxy implements InvocationHandler {
    private long mLastClickTime = 0;
    private int mDelayTime = 1000;
    private View.OnClickListener mOnClickListener;

    public ClickListenerProxy(View.OnClickListener onClickListener){
        this.mOnClickListener = onClickListener;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if("onClick".equals(method.getName())){
            long clickTime = System.currentTimeMillis();
            if (clickTime - mLastClickTime > mDelayTime) {
                mLastClickTime = clickTime;
                return method.invoke(mOnClickListener, args);
            }else{
                return null;
            }
        }else{
            return method.invoke(mOnClickListener, args);
        }
    }


    public static View.OnClickListener getProxy(View.OnClickListener onClickListener){
        ClickListenerProxy onDelayClickListener = new ClickListenerProxy(onClickListener);
        return  (View.OnClickListener) Proxy.newProxyInstance(onDelayClickListener.getClass().getClassLoader(), onClickListener.getClass().getInterfaces(),onDelayClickListener);
    }
}
